package org.nViron.nvParams;

//~--- non-JDK imports --------------------------------------------------------

import java.util.HashMap;

import org.nViron.nvParams.nvContextParamMgr.CONTEXTPARAM;

/**
 * Caches context parameters that a frequently queried but only seldomly updated
 * @author Roland Haring <p>
 * <script type="text/javascript" src="http://www.ohloh.net/p/376434/widgets/project_basic_stats.js"></script>
 */
public class nvContextValueCache {
    private final HashMap contextValues = new HashMap();
    private nvContextCalculator contextCalc;

    public nvContextValueCache(nvContextCalculator contextCalc) {
        this.contextCalc = contextCalc;
    }

    public float getContextValue(CONTEXTPARAM param) {
        Float val = (Float) contextValues.get(param);
        if (val == null){
            throw new NullPointerException("unknown context param "+param.name());
        }
        return val.floatValue();
    }

    protected void setContextValue(CONTEXTPARAM param, float val) {
        contextValues.put(param, new Float(val));
    }

    public void updateAllContextValues(nvContextParamMgr contextParamMgr) {
        for (int i = 0; i < contextParamMgr.size(); i++) {
            nvContextParam param = (nvContextParam) contextParamMgr.get(i);

            setContextValue(param.getKind(), contextCalc.calculateContextValue(param.getKind()));
        }
    }

    public void updateContextValue(CONTEXTPARAM param) {
        setContextValue(param, contextCalc.calculateContextValue(param));
    }
}



